<template>
  <div class="basic-layout">
    <div :class="['nav-side', isCollapse ? 'fold' : 'unfold']">
      <!-- LOGO -->
      <div class="logo" @click="$router.push('/')">
        <img src="../assets/images/logo.png" alt="" />
        <span>Manager</span>
      </div>
      <!-- 导航菜单 -->
      <el-menu
        :default-active="activeMenu"
        router
        class="nav-menu"
        background-color="#001529"
        text-color="#fff"
        active-text-color="#409eff"
        :collapse="isCollapse"
      >
        <tree-menu :menuList="menuList" />
        <!-- <el-submenu index="1">
          <template #title> <i class="el-icon-location"></i> <span>系统管理</span> </template>
          <el-menu-item index="1-1" class="text-center">用户管理</el-menu-item>
          <el-menu-item index="1-2" class="text-center">菜单管理</el-menu-item>
        </el-submenu>
        <el-submenu index="2">
          <template #title> <i class="el-icon-menu"></i> <span>审批管理</span> </template>
          <el-menu-item index="2-1" class="text-center"> 休假申请</el-menu-item>
          <el-menu-item index="2-2" class="text-center">待我审批</el-menu-item>
        </el-submenu> -->
      </el-menu>
    </div>
    <div :class="['content-right', isCollapse ? 'fold' : 'unfold']">
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="toggleCollapse"><i class="el-icon-s-fold"></i></div>
          <!-- 面包屑导航 -->
          <div class="bread">
            <breadcrumb />
          </div>
        </div>
        <!-- 用户信息 -->
        <div class="user-info">
          <el-badge @click="$router.push('/audit/approve')" v-auth="'approve-query'" :is-dot="noticeCount > 0" class="notice" type="danger">
            <i class="el-icon-bell"></i>
          </el-badge>
          <el-dropdown @command="handleCommand">
            <span class="user-link">
              {{ userInfo?.userName }}
              <i class="el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email">邮箱：{{ userInfo.userEmail }}</el-dropdown-item>
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>
<script>
import TreeMenu from '@/components/TreeMenu.vue';
import Breadcrumb from '@/components/Breadcrumb.vue';
import { mapState } from 'vuex';
export default {
  components: { TreeMenu, Breadcrumb },
  data() {
    return {
      // userInfo: this.$store.state.userInfo,
      // userMenu: this.$store.state.menuList,
      isCollapse: false,
      activeMenu: location.hash.slice(1),
    };
  },
  computed: { ...mapState(['userInfo', 'menuList', 'noticeCount']) },
  created() {
    this.getNoticeCount();
    this.getMenuAndActionList();
  },
  watch: {
    $route(newRoute) {
      this.activeMenu = newRoute.fullPath;
    },
  },
  methods: {
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    handleCommand(key) {
      if (key == 'email') return;
      this.$router.push('/login');
      this.$store.commit('setUserInfo', {});
    },
    async getNoticeCount() {
      let num = await this.$api.getNoticeCount();
      this.$store.commit('setNoticeCount', num);
    },
    async getMenuAndActionList() {
      // let list = await this.$api.getMenuList();
      let { menuList, actionList } = await this.$api.getUserMenu();
      this.$store.commit('setMenuList', menuList);
      // 每次登录成功了加载路由
      this.loadAsyncRoutes(menuList);
      this.$store.commit('setActionList', actionList);
    },
    loadAsyncRoutes(menuList) {
      let routes = this.$utils.generateRoute(menuList);
      routes.map((route) => {
        let url = `./../views${route.component}.vue`;
        route.component = () => import(/* @vite-ignore */ url);
        this.$router.addRoute('home', route);
      });
    },
  },
};
</script>
<style lang="scss">
.basic-layout {
  position: relative;
  .nav-side {
    position: fixed;
    height: 100vh;
    background-color: #001529;
    color: #fff;
    overflow-y: auto;
    overflow-x: hidden;
    transition: width 0.5s;
    .logo {
      display: flex;
      align-items: center;
      /* justify-content: center; */
      font-size: 18px;
      height: 50px;
      cursor: pointer;
      img {
        margin: 0 16px;
        width: 32px;
        height: 32px;
      }
    }
    .nav-menu {
      height: calc(100vh - 50px);
      border-right: none;
    }
    // 合并
    &.fold {
      width: 64px;
    }
    // 展开
    &.unfold {
      width: 200px;
    }
  }
  .content-right {
    transition: margin-left 0.5s;
    // 合并
    &.fold {
      margin-left: 64px;
      .el-icon-s-fold {
        transform: rotate(180deg);
      }
    }
    // 展开
    &.unfold {
      margin-left: 200px;
    }
    .nav-top {
      height: 50px;
      line-height: 50px;
      display: flex;
      justify-content: space-between;
      background-color: #fff;
      border-bottom: 1px solid #ddd;
      padding: 0 20px;
      .nav-left {
        display: flex;
        align-items: center;
        .menu-fold {
          margin-right: 15px;
          font-size: 18px;
        }
      }
      .user-info {
        .notice {
          line-height: 30px;
          margin-right: 15px;
          cursor: pointer;
        }
        .user-link {
          cursor: pointer;
          color: #409eff;
        }
      }
    }
    .wrapper {
      background-color: #eef0f3;
      padding: 20px;
      min-height: calc(100vh - 50px);
    }
  }
}
</style>
